package com.bifrost.util;

import com.bifrost.entity.Order;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

/**
 * 随机数生成工具
 */
public class NumberUtils {

    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyMMddHH");

    public static String getRandomNumber(int length){
        StringBuffer stringBuffer = new StringBuffer();
        Random random = new Random();
        //保证第一位不为0
        stringBuffer.append(random.nextInt(9)+1);
        for (int i=0;i<length-1;i++)
        {
            stringBuffer.append(random.nextInt(10));
        }
        return stringBuffer.toString();
    }

    /**
     * 生成订单号
     * @param order
     * @return
     */
    public static String determineOrderNumber(Order order) {
        return String.format("%s%d%d", LocalDateTime.now().format(FORMATTER), order.getMerchantStore().getMerchantId(), order.getOrderId());
    }
}
